package function;

import book.Book;
import book.BookList;
import function.Comparator.*;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class SortBooks implements IFunction{
    private Comparator[] comparators = new Comparator[]{
            new NameComparator(),
            new AuthorComparator(),
            new PriceComparator(),
            new TypeComparator(),
            new StateComparator()
    };
    public static void bubbleSort(Book[] books, int c){
        SortBooks sortBooks = new SortBooks();
        for (int i = 0; i < books.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < books.length - 1 - i; j++) {
                if(sortBooks.comparators[c].compare(books[j], books[j + 1]) > 0){
                    Book tmp = books[j];
                    books[j] = books[j + 1];
                    books[j + 1] = tmp;
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    }
    @Override
    public void act(BookList bookList) {
        System.out.println("排序书籍:");
        System.out.println("请选择排序方式:");
        System.out.println("0.按书名排序");
        System.out.println("1.按作者排序");
        System.out.println("2.按价格排序");
        System.out.println("3.按类型排序");
        System.out.println("4.按状态排序");
        Scanner scanner = new Scanner(System.in);
        int res = scanner.nextInt();
        //Book[] books = bookList.getBooks();
        //Arrays.sort(bookList.getBooks());
        bubbleSort(bookList.getBooks(), res);
        System.out.println(Arrays.toString(bookList.getBooks()));
    }
}
